<?php

abstract class empresasDAOImpl {

    public static function Select($search) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id,alertpay,id_status,id_plan,cnpj,fancyname,corporatename,
                id_city,cep,address,number,district,complement,contacts,
                filepicture,filebrand,website from enterprises ";
            if (isset($search)) {
                if (is_int($search)) {
                    $query = $query . " where id=?";
                } else if (is_string($search)) {
                    $query = $query . " where fancyname=? or corporatename=?";
                }
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_int($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else if (is_string($search)) {
                    DB::$_stmt->bind_param('ss', $search, $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_alertpay, $data_id_status, $data_id_plan, $data_cnpj, $data_fancyname, $data_corporatename, $data_id_city, $data_cep, $data_address, $data_number, $data_district, $data_complement, $data_contacts, $data_filepicture, $data_filebrand, $data_website
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'alertpay' => $data_alertpay,
                    'id_status' => $data_id_status,
                    'id_plan' => $data_id_plan,
                    'cnpj' => $data_cnpj,
                    'fancyname' => $data_fancyname,
                    'corporatename' => $data_corporatename,
                    'id_city' => $data_id_city,
                    'cep' => $data_cep,
                    'address' => $data_address,
                    'number' => $data_number,
                    'district' => $data_district,
                    'complement' => $data_complement,
                    'contacts' => $data_contacts,
                    'filepicture' => $data_filepicture,
                    'filebrand' => $data_filebrand,
                    'website' => $data_website
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function SelectCustomersVehicles() {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select e.id,e.corporatename,s.name,p.name,p.price from enterprises e
inner join status s on s.id=e.id_status
inner join plans p on p.id=e.id_plan
where (e.id_status = 1 or e.id_status = 4) and e.alertpay=1";
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_name, $data_status, $data_planame, $data_price
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'name' => $data_name,
                    'status' => $data_status,
                    'planame' => $data_planame,
                    'price' => $data_price
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Listar($limit = NULL, $page = NULL, $search = NULL) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id,alertpay,id_status,id_plan,cnpj,fancyname,corporatename,
                id_city,cep,address,number,district,complement,contacts,
                filepicture,filebrand,website from enterprises";
            if (isset($search)) {
                if (is_int($search)) {
                    $query = $query . " where id=?";
                } else if (is_string($search)) {
                    $query = $query . " where fancyname like ?";
                }
            }
            if (isset($limit)) {
                $query = $query . " limit " . $limit;
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_numeric($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else {
                    DB::$_stmt->bind_param('s', $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_alertpay, $data_id_status, $data_id_plan, $data_cnpj, $data_fancyname, $data_corporatename, $data_id_city, $data_cep, $data_address, $data_number, $data_district, $data_complement, $data_contacts, $data_filepicture, $data_filebrand, $data_website
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'alertpay' => $data_alertpay,
                    'id_status' => $data_id_status,
                    'id_plan' => $data_id_plan,
                    'cnpj' => $data_cnpj,
                    'fancyname' => $data_fancyname,
                    'corporatename' => $data_corporatename,
                    'id_city' => $data_id_city,
                    'cep' => $data_cep,
                    'address' => $data_address,
                    'number' => $data_number,
                    'district' => $data_district,
                    'complement' => $data_complement,
                    'contacts' => $data_contacts,
                    'filepicture' => $data_filepicture,
                    'filebrand' => $data_filebrand,
                    'website' => $data_website
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function UpdateStatus($id_status, $id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update enterprises set 
                id_status=?
                where id=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ii', $id_status, $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Update($id_status, $id_plan, $cnpj, $fancyname, $corporatename, $id_city, $cep, $address, $number, $district, $complement, $contatcts, $filepicture, $filebrand, $website, $id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update enterprises set 
                id_status=?,
                id_plan=?,
                cnpj=?,
                fancyname=?,
                corporatename=?,
                id_city=?,
                cep=?,
                address=?,
                number=?,
                district=?,
                complement=?,
                contacts=?,
                filepicture=?,
                filebrand=?,
                website=?
                where id=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('iidssiisissssssi', $id_status, $id_plan, $cnpj, $fancyname, $corporatename, $id_city, $cep, $address, $number, $district, $complement, $contatcts, $filepicture, $filebrand, $website, $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function UpdateAlertPay($alertpay, $id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update enterprises set 
                alertpay=?
                where id=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('ii', $alertpay, $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Insert($id_status, $id_plan, $cnpj, $fancyname, $corporatename, $id_city, $cep, $address, $number, $district, $complement, $contatcts, $filepicture, $filebrand, $website) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into exchanges
                (id, id_status,id_plan,cnpj,fancyname,corporatename,
                id_city,cep,address,number,district,complement,contacts,
                filepicture,filebrand,website) 
                values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('iidssiisissssss', $id_status, $id_plan, $cnpj, $fancyname, $corporatename, $id_city, $cep, $address, $number, $district, $complement, $contatcts, $filepicture, $filebrand, $website);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$mysqli->insert_id;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Delete($id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "delete from enterprises where id = ?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('i', $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function CreateTransaction($id, $id_enterprise) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into enterprises_transactions
                (id, id_enterprise) 
                values (?,?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('si', $id, $id_enterprise);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function SelectVehicles($search=NULL) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select v.id,m.name as model,b.name as brand, vp.filename, v.featured
                        from enterprises e 
                        inner join enterprises_vehicles ev on ev.id_enterprise=e.id
                        inner join vehicles v on v.id=ev.id_vehicle
                        inner join vehicles_pictures vp on vp.id_vehicle=v.id
                        inner join models m on m.id=v.id_model
                        inner join brands b on b.id=m.id_brand ";
            if (isset($search)) {
                if (is_int($search)) {
                    $query = $query . " where e.id=? group by v.id";
                }
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_int($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } 
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result(
                    $data_id, $data_model, $data_brand, $data_filename, $data_featured 
            );
            while (DB::$_stmt->fetch()) {
                $data[] = array(
                    'id' => $data_id,
                    'model'=>$data_model,
                    'brand'=>$data_brand,
                    'filename'=>$data_filename,
                    'featured'=>$data_featured
                );
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

}

?>
